package com.bosssoft.permission.centre.service.impl;
import com.bosssoft.permission.centre.dao.mapper.ResourceMapper;
import com.bosssoft.permission.centre.dao.mapper.RoleMapper;
import com.bosssoft.permission.centre.dao.mapper.TenantMapper;
import com.bosssoft.permission.centre.entity.Resource;
import com.bosssoft.permission.centre.service.MenuService;
import com.sd365.common.core.common.pojo.entity.TenantBaseEntity;
import com.bosssoft.permission.centre.dao.mapper.UserMapper;
import com.bosssoft.permission.centre.entity.Role;
import com.bosssoft.permission.centre.entity.User;
import com.bosssoft.permission.centre.pojo.vo.ResourceVO;
import com.bosssoft.permission.centre.service.LoginService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    TenantMapper tenantMapper;

    @Autowired
    ResourceMapper resourceMapper;

    @Autowired
    MenuService menuService;

    private static final String ADMIN_NAME="admin";

    @Override
    public int verification(String code, String password, String account) {
        User user_admin=userMapper.selectNameByCodePassword(code,password);


        if(user_admin!=null){
            //首先判断是不是管理员登录
            if(!user_admin.getName().equals(ADMIN_NAME)){
                System.out.println("");

                Long tenantId = tenantMapper.selectTenantIdByTenantAccount(account);


                // 租户ID输入错误
                try{
                    if(!tenantId.equals(user_admin.getTenantId())) {
                        return 0;
                    }
                }catch (Exception e){
                    System.out.println(e);
                }


                //User user = userMapper.selectUserByCodeTenantid(code, tenantId);
                // 租户和账号不匹配
                if(user_admin == null) {
                    return 0;
                }
                // 判断密码是否正确
                if(!user_admin.getPassword().equals(password)) {
                    return 2;
                }

            }
        }else{
            return 2;
        }



        // 判断密码是否正确
        /*if(!user_admin.getPassword().equals(password)) {
            return 2;
        }*/

        // 用户验证成功
        return 1;
    }

    @Override
    public User getUserInfo(String code, String account) {
        Long tenantId = tenantMapper.selectTenantIdByTenantAccount(account);

        System.out.println("tenantId:"+tenantId);
        User user = userMapper.selectUserByCodeTenantid(code, tenantId);
        return user;
    }

    @Override
    public List<Role> getRolesByUserId(Long userId) {
        List<Role> roles = roleMapper.selectRolesByUserId(userId);
        return roles;
    }

    @Override
    public List<ResourceVO> getResourceVO(List<Role> roles) {
        List<Long> roleIds = new LinkedList<>();
        Iterator<Role> it = roles.iterator();
        while(it.hasNext()) {
            roleIds.add(it.next().getId());
        }

        // TODO
        //List<Resource> resources = resourceMapper.getResourceByRoleIds(roleIds);
        List<Resource> resources=new ArrayList<>();
        /*for(Long id: roleIds){
            Resource resource=resourceMapper.getResourceByRoleId(id);
            resources.add(resource);
        }*/
        resources=resourceMapper.getResourceByRoleId(roleIds);
        //resources.add(resource);

        List<ResourceVO> resourceVO = menuService.buildMenu(resources);
        return resourceVO;
    }
}
